<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
          type="primary"
          @click="handleAdd"
          >添加</el-button>
      </div>
      <el-table
        :data="resourceCategories"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
      >
        <el-table-column
          type="index"
          label="编号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序">
        </el-table-column>
        <el-table-column
          width="180"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :title="category.id ? '编辑分类' : '添加分类'"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <create-or-edit
        v-if="dialogVisible"
        :category="category"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourceCategories, deleteResourceCategories } from '@/services/resource-category'
import createOrEdit from './CreateOrEdit.vue'

export default Vue.extend({
  name: 'ResourceList',
  components: { createOrEdit },
  data () {
    return {
      resourceCategories: [], // 资源分类列表
      isLoading: true, // 加载状态
      category: {},
      dialogVisible: false
    }
  },

  created () {
    this.loadResourceCategories()
  },

  methods: {
    async loadResourceCategories () {
      this.isLoading = true // 展示加载中状态
      const { data } = await getResourceCategories()
      this.resourceCategories = data.data
      this.isLoading = false // 关闭加载中状态
    },

    handleAdd () {
      this.category = {
        name: '',
        sort: 0
      }
      this.dialogVisible = true
    },

    handleEdit (item: any) {
      this.category = item
      this.dialogVisible = true
    },

    async handleDelete (item: any) {
      try {
        await this.$confirm(`确认删除分类：${item.name}？`, '删除提示')
        await deleteResourceCategories(item.id)
        this.$message.success('删除成功')
        this.loadResourceCategories()
      } catch (err) {
        if (err && err.response) {
          this.$message.error('删除失败，请重试')
        } else {
          this.$message.info('取消删除')
        }
      }
    },

    onSuccess () {
      this.dialogVisible = false // 关闭对话框
      this.loadResourceCategories() // 重新加载数据列表
    }
  }
})
</script>

<style lang="scss" scoped></style>
